package com.yhyvr.console.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderRefundGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.*;
import com.lz.tool.BeanMapTool;
import com.lz.tool.ListUtils;
import com.yhyvr.console.annotation.RepeatSubmit;
import com.yhyvr.service.ProxyOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lz.kit.LogKit;
import com.lz.tool.LzStringUtils;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import com.yhyvr.console.model.login.LoginUserModel;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderRefundMapper;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.console.controller.ControllerQueryTool;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Controller
public class BoProxyOrderRefundController{
	@Resource
	private BoProxyOrderMapper boProxyOrderMapper;
	@Resource
	private ProxyOrderService proxyOrderService;
	@Resource
	private BoProxyOrderRefundMapper boProxyOrderRefundMapper;
	@Resource
	private BoGoodsMapper boGoodsMapper;
	@Resource
	private BoProxyOrderRefundGoodsMapper boProxyOrderRefundGoodsMapper;

	@RequestMapping("/console/bo_proxy_order_refund/list")
	public String list(){
		return "console/boProxyOrderRefund/boProxyOrderRefund_table";
	}

	@RequestMapping("/console/bo_proxy_order_refund/audit")
	public @ResponseBody HttpResult auditRefundOrder(String orderId,int audit){
		try {
			return proxyOrderService.auditRefundOrder(orderId,audit);
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResp.getError();
		}
	}
	@RequestMapping("/console/bo_proxy_order_refund/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		BoProxyOrderRefundExample boProxyOrderRefundExample = new BoProxyOrderRefundExample();
        ControllerQueryTool.setBoProxyOrderRefundCriteria(bootGridReq.getQuery(),boProxyOrderRefundExample.createCriteria());
		List<BoProxyOrderRefund> boProxyOrderRefundList = boProxyOrderRefundMapper.selectByExample(boProxyOrderRefundExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(boProxyOrderRefundList);
		if(ListUtils.isNotBlank(list)){
			for (JSONObject jsonObject : list) {
				BoProxyOrder boProxyOrder = boProxyOrderMapper.selectByPrimaryKey(jsonObject.getString("orderId"));
				BoProxyOrderRefundGoodsExample boProxyOrderRefundGoodsExample = new BoProxyOrderRefundGoodsExample();
				boProxyOrderRefundGoodsExample.createCriteria().andOrderIdEqualTo(jsonObject.getString("orderRefundId"));
				BoProxyOrderRefundGoods boProxyOrderRefundGood = ListUtils.getFirst(boProxyOrderRefundGoodsMapper.selectByExample(boProxyOrderRefundGoodsExample));
				jsonObject.put("goodsName",boGoodsMapper.selectByPrimaryKey(boProxyOrderRefundGood.getGoodsId()).getGoodsTitle());
				jsonObject.put("goodsNumber",boProxyOrderRefundGood.getOrderGoodsNumber());
				jsonObject.put("orderNumber",boProxyOrder.getOrderNumber());
				jsonObject.put("orderType",boProxyOrder.getOrderType());
				switch (jsonObject.getInteger("orderAuditState")){
					case 1:
						jsonObject.put("canShenhe",true);
						break;
					case 2:
						jsonObject.put("canShouHuo",true);
						break;
					default:
						break;
				}
		}
		}
		return new BootStrapGridResp(page.getTotal(),list);
	}
}
